package com.sangfor.ssl.l3vpn.service.networkInfo;

import android.app.Application;
import android.content.Context;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.SangforAuthManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/* loaded from: classes2.dex */
public class NetworkInterceptInfoModel implements IPacketInfoListener {
    private static final String TAG = "NetworkInterceptInfoModel";
    private boolean hasUploadLog;
    private Application mApplication;
    private Object mObject;
    public Map<Integer, AppInfo> mUidAppCache;
    private Object mUidCacheObject;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static final class SingletonHolder {
        private static final NetworkInterceptInfoModel INSTANCE = new NetworkInterceptInfoModel();

        private SingletonHolder() {
        }
    }

    public static final NetworkInterceptInfoModel getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private NetworkInterceptInfoModel() {
        this.mUidAppCache = null;
        this.mObject = new Object();
        this.mUidCacheObject = new Object();
        this.hasUploadLog = false;
        this.mApplication = SangforAuthManager.getInstance().getmApplication();
        if (this.mUidAppCache == null) {
            this.mUidAppCache = new HashMap();
        } else {
            clearCache();
        }
    }

    private Context getCtx() {
        if (this.mApplication == null) {
            this.mApplication = SangforAuthManager.getInstance().getmApplication();
        }
        Application application = this.mApplication;
        if (application != null) {
            return application.getApplicationContext();
        }
        return null;
    }

    public void clearCache() {
        synchronized (this.mUidCacheObject) {
            this.mUidAppCache.clear();
        }
    }

    private String getPacketString(PacketInfo packetInfo, AppInfo appInfo) {
        if (appInfo == null) {
            return String.format("src_ip %s port %s dst_ip %s(%s) port %s protocal %s", packetInfo.srcHost, Integer.valueOf(packetInfo.srcPort), packetInfo.domain, packetInfo.host, Integer.valueOf(packetInfo.port), Integer.valueOf(packetInfo.proto));
        }
        return String.format("src_ip %s port %s dst_ip %s(%s) port %s protocal %s app_name_main %s app_name_all %s pkgs %s", packetInfo.srcHost, Integer.valueOf(packetInfo.srcPort), packetInfo.domain, packetInfo.host, Integer.valueOf(packetInfo.port), Integer.valueOf(packetInfo.proto), appInfo.leaderAppName, appInfo.allAppName, appInfo.pkgs.toString());
    }

    @Override // com.sangfor.ssl.l3vpn.service.networkInfo.IPacketInfoListener
    public void onPacketInfoCallback(PacketInfo packetInfo) {
        AppInfo createFromUid;
        if (this.mUidAppCache.containsKey(Integer.valueOf(packetInfo.uid))) {
            createFromUid = this.mUidAppCache.get(Integer.valueOf(packetInfo.uid));
        } else {
            createFromUid = getCtx() != null ? AppInfo.createFromUid(getCtx(), packetInfo.uid) : null;
            if (createFromUid == null) {
                Log.error(TAG, "uid(%d) appinfo is null", Integer.valueOf(packetInfo.uid));
            } else {
                synchronized (this.mUidCacheObject) {
                    this.mUidAppCache.put(Integer.valueOf(packetInfo.uid), createFromUid);
                }
            }
        }
        if (createFromUid == null) {
            Log.info(TAG, "error, appinfo is illegal.");
        }
        Log.info(TAG, "get info " + getPacketString(packetInfo, createFromUid));
        if (this.hasUploadLog) {
            return;
        }
        this.hasUploadLog = true;
        Log.uploadLogDelay(5);
    }

    private String getCurrentTime() {
        return new SimpleDateFormat("MM-dd HH:mm:ss").format(new Date());
    }
}
